package ui.activity.login;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.InputType;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;

import java.util.HashMap;
import java.util.Map;

import base.BaseBean;
import base.activity.BaseNetActivity;
import bean.LoginBean;
import student.andlei.com.student.R;

public class ForgetPasswordActivity extends BaseNetActivity{
    private EditText ed_mobile;
    private EditText ed_smscode;
    private EditText ed_password;
    private Button bt_smscode;
    private CheckBox check_psd;
    private Button bt_commit;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setBaseBack(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        setTitle("忘记密码");
    }

    @Override
    public void success(String action, BaseBean baseBean) {
        hideLoadDialog();
        if(action.equals("common/sendVerCode")){
            if(baseBean.code == 200){
                /** 倒计时60秒，一次1秒 */
                CountDownTimer timer = new CountDownTimer(60*1000, 1000) {
                    @Override
                    public void onTick(long millisUntilFinished) {
                        // TODO Auto-generated method stub
                        bt_smscode.setText(millisUntilFinished/1000+"秒");
                    }

                    @Override
                    public void onFinish() {
                        bt_smscode.setText("获取验证码");
                    }
                }.start();
            }else {
                toast(baseBean.msg);
            }
        }else if(action.equals("common/forgetPassword")){
            toast(baseBean.msg);
            if(baseBean.code == 200){
                toast("密码设置成功");
                finish();
            }else {
                toast(baseBean.msg);
            }
        }
    }

    @Override
    public void error(String action, Throwable e) {
        hideLoadDialog();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_forget_password;
    }

    @Override
    protected void findViews() {
        check_psd = getView(R.id.checkbox_showpassword);
        ed_mobile = getView(R.id.ed_user);
        ed_smscode = getView(R.id.ed_smscode);
        ed_password = getView(R.id.ed_password);
        bt_smscode = getView(R.id.bt_smscode);
        bt_commit = getView(R.id.bt_register);
    }

    @Override
    protected void formatViews() {
        bt_smscode.setOnClickListener(this);
        bt_commit.setOnClickListener(this);
        check_psd.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if(compoundButton.isChecked()){
                    ed_password.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                }else {
                    ed_password.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD | InputType.TYPE_CLASS_TEXT );
                }
            }
        });
    }

    @Override
    protected void formatData() {

    }

    @Override
    protected void getBundle(Bundle bundle) {

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.bt_smscode:
                Map<String,String> mapcode = new HashMap<>();
                mapcode.put("mobile",ed_mobile.getText().toString().trim());
                mapcode.put("type","forget");
                post("common/sendVerCode",mapcode, LoginBean.class,false);
                break;
            case R.id.bt_register:
                Map<String,String> map = new HashMap<>();
                map.put("mobile",ed_mobile.getText().toString().trim());
                map.put("code",ed_smscode.getText().toString().trim());
                map.put("password",ed_password.getText().toString().trim());
                post("common/forgetPassword",map, LoginBean.class,true);
                break;
        }
    }
}
